﻿using System.Diagnostics;

namespace RatingAuthor
{
  public class Book
  {
    public bool IsNew { get; set; }
    public bool IsUpdated { get; set; }
    public Author Author { get; set; }
    public string Url { get; set; }
    public string FullUrl { get { return Author.FullUrl + Url + ".shtml"; } }
    public string Title { get; set; }
    public int Size { get; set; }
    public GenresSet Ganres { get; set; }
    public double Mark { get; set; }
    public int MarksCount { get; set; }
    public int CommentsCount { get; set; }

    public override string ToString()
    {
      return Title;
    }

    public Book()
    {
    }

    internal Book(BookRecord record, GenresLookup genresLookup)
    {
      IsNew = true;
      Url = record.Url;
      Title = record.Title;
      Size = record.Size;
      Ganres = genresLookup.Lookup(record.Ganres);
      Mark = record.Mark ?? 0;
      MarksCount = record.MarksCount ?? 0;
      CommentsCount = record.CommentsCount ?? -1;
    }

    internal bool Update(BookRecord book)
    {
      var res = false;
      if (book.MarksCount != null)
      {
        var val = (int)book.MarksCount;
        res |= MarksCount != val;
        MarksCount = val;
      }
      if (book.Mark != null)
      {
        var val = (double)book.Mark;
        res |= Mark != val;
        Mark = val;
      }
      if (book.CommentsCount != null)
      {
        var val = (int)book.CommentsCount;
        res |= CommentsCount != val;
        CommentsCount = val;
      }
      IsUpdated |= res;
      return res;
    }
  }
}